package com.wyy.springcloud.controller;

import com.wyy.springcloud.pojo.Dept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController
public class DeptConsumerController {

    @Autowired
    RestTemplate restTemplate;

    //private String baseUrl = "http://localhost:8001";
    private String baseUrl = "http://SPRINGCLOUD-PROVIDER-DEPT";

    @RequestMapping("/consumer/dept/get/{id}")
    public Dept getDept(@PathVariable("id") Long id){
        return restTemplate.getForObject(baseUrl+"/dept/get/"+id,Dept.class);
    }

    @RequestMapping("/consumer/dept/get/all")
    public List<Dept> getDeptAll(){
        return restTemplate.getForObject(baseUrl+"/dept/get/all", List.class);
    }

    @RequestMapping(value = "/consumer/dept/add")
    public boolean addDept(@RequestParam String dname){
        Dept dept = new Dept(dname);
        return restTemplate.postForObject(baseUrl+"/dept/add", dept,Boolean.class);
    }

}
